package com.etop.pojo;

import java.io.Serializable;
import java.util.*;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.*;
//import org.hibernate.validator.constraints.NotEmpty;
import com.fasterxml.jackson.annotation.JsonIgnore;

@SuppressWarnings("serial")
@Entity
@Table(name="t_user")
public class User implements Serializable {
   private Integer id;
   //@NotEmpty(message="用户名不能为空")
   private String username;
   //@NotEmpty(message="密码不能为空")
   private String password;
   private Set<Role> roleList;
   
   @Id
   @GeneratedValue(strategy=GenerationType.IDENTITY)
public Integer getId() {
	return id;
}
public void setId(Integer id) {
	this.id = id;
}

public String getUsername() {
	return username;
}
public void setUsername(String username) {
	this.username = username;
}
public String getPassword() {
	return password;
}
public void setPassword(String password) {
	this.password = password;
}

@ManyToMany(fetch=FetchType.EAGER)
@JsonIgnore
//防止无限循环
@JoinTable(name="t_user_role",
		   joinColumns={@JoinColumn(name="user_id")},
		   inverseJoinColumns={@JoinColumn(name="role_id")})
public Set<Role> getRoleList() {
	return roleList;
}
public void setRoleList(Set<Role> roleList) {
	this.roleList = roleList;
}
 
@Transient
public Set<String> getRolesName(){
	Set<Role> roles=getRoleList();
	Set<String> set=new HashSet<String>();
	for(Role role:roles){
		set.add(role.getRolename());
	}
	return set;
}
  
public User(){
	super();
}
public User(Integer id,
		    String username,
		    String password,
		    Set<Role> roleList){
	super();
	this.id=id;
	this.username=username;
	this.password=password;
	this.roleList=roleList;
	
}
}
